<template>
	<view class="content">
		<image src="https://aixue-1256834579.cos.ap-shanghai.myqcloud.com/minke/e001.jpg" mode="widthFix"  @tap="sub_login()" v-if="!if_login"></image>
		<div v-else class="member-profile cl">
			<div class="member-content cl">  
				<image :src="member_info.photo"></image>
				<p>{{member_info.nickName}}12323</p>
			</div>
		</div>
		<image src="https://kehu-1256834579.cos.ap-shanghai.myqcloud.com/yuanxiang/wode.jpg" mode="widthFix" style="width: 100%;">
	</view>
</template>

<script>
	import common_config from '../../static/js/common.js';
	import WXBizDataCrypt from "@/static/js/WXBizDataCrypt.js";
	var config = common_config.config;
	var that;	
	var userInfo;
	export default {
		data() {
			return {
				if_login:false,
				openid:'',
				session_key:'',
				avatarUrl:'',
				nickName:'',
				show_login:0,
				member_info:{},
				data_info:{
					mobile:''
				},
				mobile:''
			}
		},
		onShow() {
			that = this;
			that.plusReady();
			that.load_basic();
			//that.plusReady();
		},
		methods: {
			//初始化
			plusReady(){
				var func1 = function(){
					that.no_login();
				}
				var func2 = function(){
					that.is_login();
				}
				common_config.common_if_login(func1,func2);
			},
			//未登录
			no_login(){
				that.if_login = false;
				that.weixin_login();
			},
			//已登录
			is_login(){
				that.if_login = true;
				userInfo = uni.getStorageSync('userInfo');
				userInfo = JSON.parse(userInfo);
				
				that.load_profile();
			},
			//加载数据
			load_profile(){ 
				var url = config.request_url+'/index/members/home/load_profile?token='+userInfo.token;
				uni.request({
				    url: url,
				    header: {
				         "Content-Type": "application/x-www-form-urlencoded",
				    },
					method:'POST',
				    success: (res) => {
						uni.hideLoading();
						res = res.data;
				        if(res.code == 200){
							that.member_info = res.data;
						}else{
							
						}
				    }
				});
			},
			//加载基本资料
			load_basic(){ 
				var url = config.request_url+'/index/index/load_basic';
				uni.request({
				    url: url,
				    header: {
				         "Content-Type": "application/x-www-form-urlencoded",
				    },
					method:'POST',
				    success: (res) => {
						uni.hideLoading();
						res = res.data;
				        if(res.code == 200){
							that.data_info = res.data;
						}
				    }
				});
			},
			//微信登录
			weixin_login(){
				uni.showLoading({
					title:'正在初始化...'
				});
				uni.login({
				    provider: 'weixin',
				    success: function (loginRes) {
						console.log(loginRes.code);
						uni.hideLoading();
				        //2.将用户登录code传递到后台置换用户SessionKey、OpenId等信息
				        uni.request({
				        	url: config.request_url+'/index/user/load_user_profile',
				        	data: {
				        		code: loginRes.code,
				        	},
				        	method: 'GET',
				        	header: {
				        		'content-type': 'application/json'
				        	},
				        	success: (res) => {
				        		res = res.data;
				        		console.log(res);
				        		that.openid = res.data.openid;
				        		that.session_key = res.data.session_key;
								if(!that.if_login){
									that.sub_login();
								}
				        	}
				        });
				    },
				    fail: function (err) {
						uni.hideLoading();
				        // 登录授权失败  
				        // err.code是错误码
				    }
				});
			},
			getPhoneNumber(PhoneNumber) {
				//common_config.calert(config.mini_appid);
			   let pc = new WXBizDataCrypt(config.mini_appid,that.session_key);
			   let data = pc.decryptData(PhoneNumber.detail.encryptedData, PhoneNumber.detail.iv);
			   
			   that.mobile = data.phoneNumber;
			   console.log(that.mobile);
			},
			test1(){
				uni.showToast({
					title:'上线后可用'
				});
			},
			param_to_url(url){
				uni.navigateTo({
					url:url
				});
			},
			//打开授权登录弹出
			open_login_wind(n){
				that.show_login =n;
			},
			//提交登录
			sub_login(){
				uni.showLoading({
					title:'正在登录...'
				});
				var url = config.request_url+'/index/user/login_check';
				uni.request({
					url:url,
					data: {
						openid: that.openid
					},
					header: {
					     "Content-Type": "application/x-www-form-urlencoded"
					},
					method:'POST',
					success: (res) => {
						uni.hideLoading();
						res = res.data;
						if(res.code == 200){
							//将登录信息写入缓存
							var userInfo2 = JSON.stringify(res.data);
							uni.setStorageSync('userInfo',userInfo2);
							userInfo = res.data;
							uni.showToast({
							    title: '登录成功',
							    duration: 2000
							});
							that.if_login = true;
							that.load_profile();
							
						}else{
							that.open_login_wind(1);
						}
					}
				});
			},
			//选择头像
			onChooseavatar(e) {
				let self = this;
				let {
					avatarUrl
				} = e.detail;
				this.avatarUrl = avatarUrl;
				that.upload_photo(1);
			},
			//上传头像
			upload_photo(n){
				var url = config.request_url+'/index/user/upload_photo';
				
				uni.uploadFile({
					url:url,
					method:"POST",
					filePath:that.avatarUrl,
					name:'file',
					formData:{
						file:that.avatarUrl
					},
					success: (res) =>{
						//uni.hideLoading();
						res = res.data;
						res = JSON.parse(res);
						console.log(res);
						if(res.code == 200){
							that.avatarUrl = res.data.src;
						}else{
							common_config.calert(res.msg);
						}
					}
				});
			},
			//表单输入事件
			onKeyEvent:function(event,the_id){
				that[the_id] = event.target.value;
			},
			//提交绑定
			sub_bind(){
				if(!that.avatarUrl){
					common_config.calert('请上传头像');
					return false;
				}
				uni.showLoading({
					title:'正在登录...'
				});
				var url = config.request_url+'/index/user/sub_login';
				uni.request({
					url:url,
					data: {
						share_uid:that.share_uid,
						openid: that.openid,
						avatarUrl:that.avatarUrl,
						nickName:that.nickName,
						mobile:that.mobile
					},
					header: {
					     "Content-Type": "application/x-www-form-urlencoded"
					},
					method:'POST',
					success: (res) => {
						uni.hideLoading();
						res = res.data;
						if(res.code == 200){
							//将登录信息写入缓存
							var userInfo2 = JSON.stringify(res.data);
							uni.setStorageSync('userInfo',userInfo2);
							userInfo = res.data;
							uni.showToast({
							    title: '登录成功',
							    duration: 2000
							});
							that.if_login = true;
							that.open_login_wind(0);
							that.load_profile();
						}else{
							common_config.calert(res.msg);
						}
					}
				});
			},
			make_call(){
				var mobile = that.data_info.mobile;
				uni.makePhoneCall({
				  phoneNumber: mobile, // 需要拨打的电话号码
				  success: () => {
				    console.log("拨打电话成功");
				  },
				  fail: (err) => {
				    console.log("拨打电话失败", err);
				  },
				  complete: () => {
				    console.log("拨打电话完成");
				  }
				});
			}
		}
	}
</script>

<style>
	@import url("./me.css");
	page,.content{background-color: #f2f5ff;}
	.content image{width: 100%;}
	
	.member-content{width: 100%;text-align: center;padding-bottom: 20rpx;margin-top: 20rpx;}
	.member-content image{width: 160rpx;height: 160rpx;border-radius: 100%;}
	.member-content p{margin-top: 20rpx;}
	/*用户信息弹窗*/
	.member-wind{width: 100%;height: 100%;left: 0px;top: 0px;position: fixed;background-color: rgba(0,0,0,0.3);z-index: 50;}
	.member-wind-inner{position: fixed;bottom: 0px;background-color: #fff;width: 94%;padding: 20rpx 3%;padding-bottom: 40rpx;z-index: 51;}
	.icon-guanbi{position: absolute;right: 20rpx;top: 20rpx;font-size: 46rpx;z-index: 12;}
	.upload-btns{width: 30%;margin-left:35%;border: 0px;background-color: #fff;border-right: 1px solid #ddd}
	.member-round{margin-top: 20rpx;text-align: center;width: 100%;position: relative;}
	.member-round image{border: 1px solid #ddd;border-radius: 100%;margin-top: 20rpx;width: 160rpx;height: 160rpx;}
	.member-round .photo-txt{position: absolute;font-size: 28rpx;top: 45%;left:10%;color: #666;}
	.nick-round{width: 100%;text-align: center;margin-top: 20rpx;position: relative;}
	.nick-round input{background-color: #f3f3f3;padding: 20rpx 2%;border-radius: 6rpx;font-size: 28rpx;width:100%;}
	.login-member{margin-top:40rpx;display: inline-block;width: 80%;margin-left:10%;background-color: #154bad;text-align: center;padding:20rpx 0px;color: #fff;border-radius: 12rpx;}
	.nick-round .send-code{display: inline-block;background-color: #fff;border:1px solid #154bad;color: #154bad;font-size: 24rpx;border-radius: 6rpx;width: 160rpx;padding:10rpx 0px;text-align: center;display: inline-block;float:left;margin-left: 10rpx;margin-top:12rpx;right:2%;position: absolute;top: 0px;z-index: 10;}
</style>
